﻿using Microsoft.Xna.Framework;

namespace Syngine.Components
{
	public class Camera : Updatable
	{
		private Vector3 _position;

		public float Scale { get; set; }

		public float Rotation { get; set; }
		
		public float MoveSpeed { get; set; }
		
		public Vector2 Origin { get; set; }
		
		public Matrix Projection { get; set; }

		public Vector2 Focus { get; set; }

		public Vector3 Position
		{
			get
			{
				return _position;
			}
			set
			{
				_position = value;
			}
		}
		
		public Vector2 Center { get; protected set; }

		public override void Initialize()
		{
			var viewPort = GameContext.Game.GraphicsDevice.Viewport;
			
			Center = new Vector2(viewPort.Width / 2.0f, viewPort.Height / 2.0f);
			Position = new Vector3(Center, 0);
			Scale = 1f;
			MoveSpeed = 1.35f;

			base.Initialize();
		}

		protected override void UpdateCore(UpdateContext context)
		{
			Origin = Center / Scale;
			Projection = Matrix.Identity *
						Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
						Matrix.CreateRotationZ(Rotation) *
						Matrix.CreateTranslation(Origin.X, Origin.Y, 0) *
						Matrix.CreateScale(new Vector3(Scale, Scale, Scale));

			var delta = (float)context.GameTime.ElapsedGameTime.TotalSeconds;
			_position.X += (Focus.X - Position.X) * MoveSpeed * delta;
			_position.Y += (Focus.Y - Position.Y) * MoveSpeed * delta;
		}
	}
}
